package service

import (
	"context"
	"mini-app/config/mysql"
	"mini-app/internal/dao"
	"mini-app/internal/models"

	"192.168.1.75/go-pkg/errorx"
)

type UserAddress struct{}

func (UserAddress) Create(
	ctx context.Context,
	openid string,
	record models.UserAddress,
) (id string, err error) {
	record.OpenID = openid
	if record.UserName == "" {
		return "", errorx.New("姓名不能为空", -1)
	}
	if record.TelNumber == "" {
		return "", errorx.New("手机号不能为空", -1)
	}
	if record.ProvinceName == "" {
		return "", errorx.New("省份不能为空", -1)
	}
	if record.CityName == "" {
		return "", errorx.New("城市不能为空", -1)
	}
	if record.DetailAddress == "" {
		return "", errorx.New("详细地址不能空", -1)
	}
	id, err = dao.UserAddress{}.Create(ctx, mysql.NewDB(), record)
	if err != nil {
		return "", errorx.New("新增收到地址失败", -1)
	}
	return
}
func (UserAddress) Delete(
	ctx context.Context,
	openid,
	id string,
) error {
	success := dao.UserAddress{}.Delete(ctx, mysql.NewDB(), openid, id)
	if !success {
		return errorx.New("地址删除失败", -1)
	}
	return nil
}
func (UserAddress) Update(
	ctx context.Context,
	openid string,
	record models.UserAddress,
) error {
	if record.ID == "" {
		return errorx.New("地址id不能为空", -1)
	}
	success := dao.UserAddress{}.Update(ctx, mysql.NewDB(), openid, record)
	if !success {
		return errorx.New("地址更新失败", -1)
	}
	return nil
}
func (UserAddress) List(
	ctx context.Context,
	openid string,
) []models.UserAddress {
	return dao.UserAddress{}.List(ctx, mysql.NewDB(), openid)
}
